package engine.control
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
      
        public class KitaWebServices{
                //
                private var url:String = new String();
                private var ns:String = new String();
                
                private var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
                private var soapXML:XML = <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
            <soap:Body/>
            </soap:Envelope>
            ;
            private var elementXML:XMLList;
            
            private var ready:Boolean = false;
            private var cache:Array = new Array();
                private var shareObj:Object = new Object();
            
                public function KitaWebServices(_url:String)
                {
                        url = _url;
                        analyseWSDL();
                }
                
                private function analyseWSDL():void
                {
                        var loader:URLLoader = new URLLoader();
                        loader.dataFormat = URLLoaderDataFormat.TEXT;
                        
                        var rqa:URLRequest = new URLRequest();
                        rqa.url = url;
                        rqa.method = URLRequestMethod.POST;
                        loader.addEventListener(Event.COMPLETE,analyseComlete);
                        loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
                        loader.load(rqa);
                }
                
                private function errorHandler(evt:IOErrorEvent):void
                {
//                        trace(evt.type);
                }
                
                private function analyseComlete(evt:Event):void
                {
                        var loader:URLLoader = evt.target as URLLoader;
                        //trace(loader.data);
                        if((loader.data as String).indexOf("Invalid SOAP request") != -1)	return;
                        var temp0:XML = XML(loader.data);
                        var wsdl:Namespace = temp0.namespace();
                        var s:Namespace;
                        for (var i:uint = 0; i < temp0.namespaceDeclarations().length; i++) {
                            s = temp0.namespaceDeclarations()[i]; 
                            var prefix:String = s.prefix;
                                   if (prefix == "xsd") {
                                //trace(s.uri);
                                break;
                            }
                                   //trace(prefix + ":" , s.uri);
                        }
                        //运行时解除!
                        elementXML = temp0.wsdl::["types"].s::["schema"];
                        ns = elementXML.@targetNamespace;
                        
                        var len:uint = elementXML.s::element.length();
                        var item:String = new String();
                        var itemLen:uint = 0;
                        var j:uint = new uint();
                        for (i =0;i<len;i+=2)
                        {
                                item = elementXML.s::element[i].@name;
                                shareObj[item] = new Array();
//								trace(item);
                                itemLen = elementXML.s::element[i].s::complexType.s::sequence.s::element.length();
                                for (j =0;j<itemLen;j++)
                                {
                                        shareObj[item].push(elementXML.s::element[i].s::complexType.s::sequence.s::element[j].@name);
                                }
                                //trace(shareObj[item]);
                                //trace(itemLen);
                                //
                        }
                        ready = true;
                        for each(var cacheLoader:WebServicesLoader in cache)
						{
                            cacheLoader.active(ns,shareObj[cacheLoader.method]);
                   		}
                    cache = new Array();
                }
                //method 方法
                public function call(_method:String,_returnMethod:Function,...args:Array):void
                {
                    var loader:WebServicesLoader  = new WebServicesLoader(url,soapXML,_method,_returnMethod,args);
                    
                    if(ready){
                            loader.active(ns,shareObj[_method]);
                    }else
                    {
                            cache.push(loader);
                    }
                }
        }
}
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;

class WebServicesLoader extends URLLoader
{
        private var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
        private var rq:URLRequest = new URLRequest();
        private var callBack:Function;
        private var url:String;
        private var soapXML:XML;
        public var method:String;
        private var args:Array;
        
        public function WebServicesLoader(_url:String,_soapXML:XML,_method:String,_returnMethod:Function,_args:Array)
        {
                url = _url;
                soapXML = XML(_soapXML.toString());
                method = _method;
                args = _args;
                
                dataFormat = URLLoaderDataFormat.TEXT;
                
                callBack = _returnMethod;
        }
        public function active(ns:String,argLabels:Array):void
        {
                //trace(argLabels);
                var methodXML :XML = XML("<"+method+" xmlns=\""+ns+"\"/>");
                var len:uint = argLabels.length;

                for (var i:uint =0;i <len;i++)
                {
                        methodXML.appendChild(XML("<"+argLabels[i]+">"+args[i]+"</"+argLabels[i]+">"));
                }
                
                var tempXML:XML = soapXML;
                //运行时解除!
                tempXML.soap::["Body"].appendChild(methodXML);
                
                
                var nns:String = ns;
                
                if(nns.lastIndexOf("/")==nns.length-1){
                        nns = nns.substr(0,nns.length-1);
                }
                
                rq.url = url+"?op="+method;                        
                rq.method = URLRequestMethod.POST;
                rq.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
                
                
                rq.requestHeaders.push(new URLRequestHeader("SOAPAction", nns+"/"+method));
        		rq.data =tempXML;
                
                addEventListener(Event.COMPLETE,comleteHandler);
                load(rq);
        }
        public function comleteHandler(evt:Event):void
        {
                var xml:XML = XML(data);
                if(callBack!=null){
//                        callBack.apply(null,new Array(xml.soap::Body.child(0).child(0)));
					callBack.apply(null,[xml]);
                }
        }
}
